<?php

defined('SYSPATH') or die('Access error!');

class Controller_Admin_Category extends Controller_Admin_Main
{

    private $category;

    public function before()
    {
        parent::before();
        $this->category = Model_Category::factory('category');
    }

    public function action_add()
    {
        $id = intval($this->request->param('id'));

        if ($_POST)
        {
            $data = array();
            $data['name'] = Arr::get($_POST, 'name', '');
            $parent_id_arr = $_POST['parent_id'];
            if (Arr::validate_arr($parent_id_arr))
            {
                $parent_id = $this->handle_parent_id($parent_id_arr);
            }
            else
            {
                $parent_id = 0;
            }
            $data['parent_id'] = $parent_id;
            $data['is_show'] = Arr::get($_POST, 'is_show', 1);
            $data['title'] = Arr::get($_POST, 'title', '');
            $data['keywords'] = Arr::get($_POST, 'keywords', '');
            $data['description'] = Arr::get($_POST, 'description', '');
            $data['position'] = Arr::get($_POST, 'position', 9999);
            $data['add_time'] = time();
            $data['update_time'] = time();
            $data['is_hot'] = Arr::get($_POST, 'is_hot');
            $data['is_best'] = Arr::get($_POST, 'is_best');

            /*             * ***************************条件判断************************************** */
            if ($data['name'] == '')
            {
                Message::set(Message::ERROR, '分类名称不能为空');
                $this->request->redirect(Route::get('admin/category')->uri(array('action' => 'add')));
            }
            if ($this->category->is_exist(array('name' => $data['name']), 0, 'parent_id=' . $data['parent_id']))
            {
                Message::set(Message::ERROR, "\"" . $data['name'] . "\"" . '--分类名称已经存在');
                $this->request->redirect(Route::get('admin/category')->uri(array('action' => 'add')));
            }


            if ($this->category->insert($data))
            {
                Message::set(Message::SUCCESS, "\"" . $data['name'] . "\"" . '--分类添加成功');
                $this->request->redirect(Route::get('admin/category')->uri(array('action' => 'add', 'id' => $this->category->get_id())));
            }
        }


        if ($id == 0)
        {
            $select = "<select name='parent_id[]' id='parent_id'  onchange='change_category(this)'>
                                        <option value='0'>==根目录==</option>
                                        " . $this->category->get_category_option('parent_id=0') . "
                                    </select>	";
            $this->template->assign('category_option', $select);
        }
        else
        {
            $parent_id = $this->category->get_var(array('id' => $id), 'parent_id');
            $this->template->assign('category_option', $this->category->get_menu_option($id, $parent_id));
        }
        $this->template->view('category_add');
    }

    public function action_list()
    {
        $id = intval($this->request->param('id'));
        $category_list_temp = $this->category->get_category_list('parent_id=' . $id);
        $category_list = array();
        if (Arr::is_array($category_list_temp))
        {
            foreach ($category_list_temp as $key => $value)
            {
                $parent = $this->category->get_row(array('id' => $value['parent_id']), 'name,parent_id');
                $parent = count($parent) > 0 ? $parent : array('name' => '无', 'parent_id' => 0);
                $value['double_parent_id'] = $parent['parent_id'];
                $value['has_child'] = $this->category->get_var(array('parent_id' => $value['id']), 'COUNT(*)');
                $value['parent_name'] = $parent['name'];
                $category_list[$key] = $value;
            }
        }
        unset($category_list_temp);
        //为搜索得到一级分类
        $seach_category = $this->category->get_category_option('parent_id=0', "<option value=\$id >\$spacer\$name</option>");
        $this->template->assign('seach_category', $seach_category);
        $this->template->assign('action', URL::site() . 'admin/category/batch_position/' . $id);
        if ($id != 0)
        {
            $parent_id = $this->category->get_row(array('id' => $id), 'parent_id');
            $this->template->assign('parent_id', $parent_id['parent_id']);
        }

        $this->template->assign('current_id', $id);
        $this->template->assign('category_list', $category_list);
        $this->template->view('category_list');
    }

    public function action_edit()
    {
        $id = intval($this->request->param('id'));
        $category = $this->category->get_row(array('id' => $id), 'name,parent_id,title,keywords,description,position,is_show,is_hot,is_best');
        if (!count($category) > 0)
        {
            Message::set(Message::ERROR, "\"$category[name]\"" . '--该分类不存在');
            $this->request->redirect(Route::get('admin/category')->uri(array('action' => 'list')));
        }
        if ($_POST)
        {
            $data = array();
            $data['name'] = Arr::get($_POST, 'name', '');
            $parent_id_arr = $_POST['parent_id'];
            if (Arr::validate_arr($parent_id_arr))
            {
                $parent_id = $this->handle_parent_id($parent_id_arr);
            }
            else
            {
                $parent_id = 0;
            }
            $data['parent_id'] = $parent_id;
            $data['is_show'] = Arr::get($_POST, 'is_show', 1);
            $data['title'] = Arr::get($_POST, 'title', '');
            $data['keywords'] = Arr::get($_POST, 'keywords', '');
            $data['description'] = Arr::get($_POST, 'description', '');
            $data['position'] = Arr::get($_POST, 'position', 9999);
            $data['update_time'] = time();
            $data['is_hot'] = Arr::get($_POST, 'is_hot');
            $data['is_best'] = Arr::get($_POST, 'is_best');

            /*             * ******************************条件判断************************************** */
            if ($data['name'] == '')
            {
                Message::set(Message::ERROR, '分类名称不能为空');
                $this->request->redirect(Route::get('admin/category')->uri(array('action' => 'add')));
            }
            if ($this->category->is_exist(array('name' => $data['name']), $id, 'parent_id=' . $data['parent_id']))
            {
                Message::set(Message::ERROR, "\"" . $data['name'] . "\"" . '--分类名称已经存在');
                $this->request->redirect(Route::get('admin/category')->uri(array('action' => 'add')));
            }


            if ($this->category->update($data, array('id' => $id)))
            {
                Message::set(Message::SUCCESS, "\"" . $data['name'] . "\"" . '--分类更新成功');
                $this->request->redirect(Route::get('admin/category')->uri(array('action' => 'edit', 'id' => $id)));
            }
        }

        $this->template->assign('action', URL::site() . 'admin/category/edit/' . $id);
        $this->template->assign('data', $category);
        if ($category['parent_id'] == 0)
        {
            $select = "<select name='parent_id[]' id='parent_id'  onchange='change_category(this)'>
                                        <option value='0'>==根目录==</option>
                                        " . $this->category->get_category_option('parent_id=0') . "
                                    </select>	";
            $this->template->assign('category_option', $select);
        }
        else
        {
            $this->template->assign('category_option', $this->category->get_menu_option($id, $category['parent_id']));
        }
        $this->template->view('category_edit');
    }

    public function action_batch_position()
    {
        $id = intval($this->request->param('id'));
        $position_arr = isset($_POST['position']) ? $_POST['position'] : array();
        if (count($position_arr) > 0)
        {
            foreach ($position_arr as $key => $value)
            {
                $this->category->update(array('position' => $value), array('id' => $key));
            }
        }

        Message::set(Message::SUCCESS, '分类排序成功');
        $this->request->redirect(Route::get('admin/category')->uri(array('action' => 'list', 'id' => $id)));
        $this->template->view('category_edit');
    }

    public function action_delete()
    {
        $id = intval($this->request->param('id'));
        $category = $this->category->get_row(array('id' => $id), 'name,parent_id');
        if (!count($category) > 0)
        {
            Message::set(Message::ERROR, "\"$category[name]\"" . '--该分类不存在');
            $this->request->redirect(Route::get('admin/category')->uri(array('action' => 'list')));
        }
        $has_child = $this->category->get_var(array('parent_id' => $id), 'COUNT(*)');
        if (!empty($has_child))
        {
            Message::set(Message::ERROR, "\"$category[name]\"" . '--该分类下有子分类，请先删除子分类');
            $this->request->redirect(Route::get('admin/category')->uri(array('action' => 'list', 'id' => $category['parent_id'])));
        }

        if (!$this->category->delete(array('id' => $id)))
        {
            Message::set(Message::SUCCESS, "\"$category[name]\"" . '--分类删除失败');
            $this->request->redirect(Route::get('admin/category')->uri(array('action' => 'list', 'id' => $category['parent_id'])));
        }
        Message::set(Message::SUCCESS, "\"$category[name]\"" . '--分类删除成功');
        $this->request->redirect(Route::get('admin/category')->uri(array('action' => 'list', 'id' => $category['parent_id'])));
    }

    public function action_seach()
    {
        $where = ' 1=1 ';
        $str = '搜索相关信息：';
        $keyword = Arr::get($_POST, 'keyword', ' ');
        $seach_select_category = Arr::get($_POST, 'seach_select_category', 0);
        $category_name = $this->category->get_var(array('id' => $seach_select_category), 'name');
        if ($seach_select_category != 0)
        {
            $str .=" 选择的分类(" . $category_name . ")&nbsp;&nbsp;";
            $where .= " AND parent_id=" . $seach_select_category;
        }

        if ($keyword != '')
        {
            $str .="关键词(" . $keyword . ")&nbsp;&nbsp;";
            $where .= " AND name LIKE '%" . $keyword . "%'";
        }
        $seach_category_list = $this->category->get_category_list($where);
        Message::set(Message::INFO, $str . "共搜索到(" . count($seach_category_list) . ")");
        $seach_category = $this->category->get_category_option('parent_id=0', '', $seach_select_category);
        $this->template->assign('seach_category', $seach_category);
        $this->template->assign('category_list', $seach_category_list);
        $this->template->view('category_list');
    }

    public function action_left()
    {
        $left_arr[0]['name'] = '分类管理';
        $left_arr[0]['sub'] = array(
            array("name" => "添加分类", "url" => URL::site() . "admin/category/add", 'id' => 20000),
            array('name' => '分类列表', 'url' => URL::site() . 'admin/category/list', 'id' => 20002),
        );
        $this->template->assign('left_arr', $left_arr);
        $this->template->view('left');
    }

    public function action_createfile()
    {
        $id = intval($this->request->param('id'));
        $category_id_list = Model::factory('category')->get_all_children($id);
        $category_list = array();
        if (Arr::validate_arr($category_id_list))
        {
            foreach ($category_id_list as $key => $value)
            {
                $category_list[$value] = $this->category->get_row(array('id' => $value), 'name,parent_id,id,keywords,title,description');
            }
        }

        $file_path = SYSTEM_CATEGORY_CACHE_PATH . 'category_'.$id.'.php';
        if (Cache::instance()->write($file_path, $category_list))
        {
            Message::success('写入成功');
            $this->request->redirect(URL::site('admin/category/list'));
        }
    }

}

